package cn.imnaer.service.rest.schedule.impl;

import cn.imnaer.common.factory.JacksonUtil;
import cn.imnaer.common.factory.ObjectMappingCustomer;
import cn.imnaer.service.persistence.model.Schedule;
import cn.imnaer.service.persistence.model.consts.SCHEDULE;
import cn.imnaer.service.rest.resquest.model.AppendScheduleParams;
import cn.imnaer.service.rest.resquest.model.ScheduleCreateParams;
import cn.imnaer.service.rest.schedule.ScheduleComponentRestApi;
import cn.imnaer.service.rest.view.model.Response;
import cn.imnaer.service.rest.view.model.SimpleResponse;
import cn.imnaer.service.rest.view.model.SimpleSchedule;
import cn.imnaer.service.rpc.schedule.ScheduleComponentRpcApi;
import cn.imnaer.service.rpc.schedule.impl.ScheduleComponentRpcApiImpl;

import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebService;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebService
public class ScheduleComponentRestApiImpl implements ScheduleComponentRestApi {
	 private static Logger LOGGER = LoggerFactory.getLogger(ScheduleComponentRestApiImpl.class);
    public ScheduleComponentRpcApi scheduleComponentRpcApi;

    public ScheduleComponentRestApiImpl() {
        scheduleComponentRpcApi = new ScheduleComponentRpcApiImpl();
    }


    public String create(String scheduleCreateParamsJson) {
        LOGGER.debug("创建行程:" + scheduleCreateParamsJson);
        ScheduleCreateParams scheduleCreateParams = ScheduleCreateParams.parserToModel(scheduleCreateParamsJson);
        Schedule schedule = scheduleComponentRpcApi.create(scheduleCreateParams);
        HashMap<String, String> record = new HashMap<String, String>();
        record.put(SCHEDULE.scheduleId, schedule.getScheduleId());
        return new SimpleResponse().success(record);
    }

    public String getMyScheduleList(String userId, String page) {
        List<SimpleSchedule> record = scheduleComponentRpcApi.getScheduleList(userId);
        int totalCount = record.size();
        if (totalCount > 0) {
            return new Response().success(record, totalCount);
        }
        return new SimpleResponse().dataEmpty();
    }


    public String deleteSchedule(String jsonData) {
        LOGGER.debug(jsonData);
        ObjectMappingCustomer mapper = JacksonUtil.getMapperInstance(false);
        try {
            List<String> list = mapper.readValue(jsonData,
                    new TypeReference<List<String>>() {
                    });
            if (list != null && list.size() > 0) {
                System.out.println(list.size());
                scheduleComponentRpcApi.deleteSchedule(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new SimpleResponse().jsonException();
        }
        return new SimpleResponse().success();


    }


    public String updateSchedule(String scheduleJson) {
        LOGGER.debug("修改行程:" + scheduleJson);
        Schedule schedule = (Schedule) JacksonUtil.jsonToBean(scheduleJson, Schedule.class);
        scheduleComponentRpcApi.update(schedule);
        return new SimpleResponse().success();
    }

    public String getScheduleDetails(String scheduleId) {
        Schedule schedule = scheduleComponentRpcApi.getScheduleDetails(scheduleId);
        return new SimpleResponse().success(schedule);
    }

    @Override
    public String append(String scheduleAppendParamsJson) {
        LOGGER.debug("追加行程:" + scheduleAppendParamsJson);
        AppendScheduleParams params =
                (AppendScheduleParams) JacksonUtil.jsonToBean(scheduleAppendParamsJson, AppendScheduleParams.class);
        scheduleComponentRpcApi.append(params);
        return new SimpleResponse().success();
    }


}
